import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;


public class Client extends Thread
{
	Socket myhost = null;	
	
	public Client(Socket host)
	{
		myhost =  host;
	}
	
	
public static void main (String args[]) throws Exception 
	{
		System.out.println ("Client program");
		
		int port=15011;
		String ip=new String("192.168.5.173");
		InetAddress ipad = InetAddress.getByName(ip);		 
		Socket client = new Socket(ipad,port);
//		(new Client(connected)).start();
		BufferedReader inc  = new BufferedReader (new InputStreamReader(client.getInputStream()));
        PrintWriter outc = new PrintWriter(new OutputStreamWriter(client.getOutputStream()));
		
        //System.out.println ("Client Waiting for Server on port "+ port);
		
        BufferedReader ids = new BufferedReader(new FileReader("./src/ids.txt"));
		int client_id=Integer.parseInt(ids.readLine());
		
		//System.out.println (client_id);
		
		BufferedReader inputStream = new BufferedReader(new FileReader("./src/list_client.txt"));
		int no_net=Integer.parseInt(inputStream.readLine());
		
		//System.out.println (no_net);
		
		int [] gr_ids=new int[no_net];
		for(int i=0;i<no_net;i++)
		{
			gr_ids[i]=Integer.parseInt(inputStream.readLine());
		
			//System.out.println (gr_ids[i]);
		}
		// send the unique client id
		
		String c=String.valueOf(client_id);
		 outc.println(c);
    	 outc.flush();   	 
    	 
    	 String response;
    	 response=inc.readLine();
    	 if(response.contains("q"))
    	 {
    		 System.out.println("User Authentication Failed");
    		 System.out.println("Client Exits");
    		 System.exit(0);
    	 }
    	 System.out.println(response);
   /* 	 if(client_id==Integer.parseInt(response))
    	 {
    		 System.out.println("id match!!!!");
    	 }
    	 
    	 //send the no of groups
    	 String ng=String.valueOf(no_net);
		 outc.println(ng);
    	 outc.flush();
    	 response=inc.readLine();
    	 System.out.println(response);
    	 if(no_net==Integer.parseInt(response))
    	 {
    		 System.out.println("hip hip hurray!!!!");
    	 }
    	 //send the group_ids
    	 for (int i=0;i<no_net;i++)
    	 {
    		 String gi=String.valueOf(gr_ids[i]);
    		 outc.println(gi);
        	 outc.flush();
        	 response=inc.readLine();
        	 System.out.println(response);
        	 if(gr_ids[i]==Integer.parseInt(response))
        	 {
        		 System.out.println("hip hip hurray!!!!");
        	 }
    	 }
    */	
    	 // SENDING SYN AND STATUS BITS
    	 
    	 System.out.println("Do you want your data to be synced with your friends ? (0/1)");
    	 int syn;
    	 Scanner in = new Scanner(System.in);
    	 syn= in.nextInt();
    	 outc.println(syn);
    	 outc.flush();	 
    	 
    	 create_non_existing_gr_folders(gr_ids,no_net);
		// list_the_folder(gr_ids,no_net);
		  String temp;
		  BufferedWriter sync_ip = new BufferedWriter(new FileWriter("list_sync_ip.txt"));
		  while(true)
		 {	temp=inc.readLine();
			 System.out.println(temp);
				sync_ip.write(temp);
			 	sync_ip.newLine();
			if(temp.compareTo("a")==0)
				 break;
		 }
		 
		 sync_ip.close();
		  //
		 outc.close();
         inc.close();
         System.out.println("Do you want to start transfer ? (0/1)");
         int t=0;
    	 Scanner intr = new Scanner(System.in);
    	 t= intr.nextInt();
    	// outc.println(syn);
    	// outc.flush();	 
        if(t==1)
        {
        //	Transfer.read();
        }
         client.close();
		 
	}

private static void create_non_existing_gr_folders(int[] gr_ids, int no_net) 
{
	try
	{
		for (int i=0;i<no_net;i++)
		{
	    String dirname =String.valueOf(gr_ids[i]);
	    boolean success = (new File(dirname)).mkdir();
	    if (success) 
	    {
	        System.out.println("Directory: " + dirname + " created");
	    }   
		}
	 }
	catch (Exception e)
	 {
	      System.err.println("Error: " + e.getMessage());
	 }
	
}

@SuppressWarnings("unused")
private static void list_the_folder(int[] gr_ids, int no_net) 
{
	 String path = "D:\\IIITD\\4th Sem\\CN\\Project\\Project\\";
	  for (int k=0;k<no_net;k++)
	  {
		  String path1=path;
		  System.out.println("---------------------");
		  System.out.println(gr_ids[k]);
		  path1=path1+String.valueOf(gr_ids[k]);
		  String files;
		  File folder = new File(path1);
		  File[] listOfFiles = folder.listFiles(); 
		  for (int i = 0; i < listOfFiles.length; i++) 
		  {
			  if (listOfFiles[i].isFile()) 
			  {
				  files = listOfFiles[i].getName();
				  System.out.println(files);
			  }
		  }
	  }
}
	
}
